package com.example.audiostreaming.views;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.audiostreaming.R;
import com.example.audiostreaming.adapters.AdapterPlayerPlayListRv;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.util.List;

import static com.example.audiostreaming.base.BaseApplication.getAppContext;

/**
 * 弹出窗口视图
 */
public class ViewPopWindow extends PopupWindow {

    private final View mPopView;
    private TextView mPlayListCloseBtn;
    private RecyclerView mPlayListRv;
    private AdapterPlayerPlayListRv mAdapterPlayerPlayListRv;
    private TextView mPlayListModeSwitchTv;
    private ImageView mPlayListModeSwitchIv;
    private View mPlayListPalyModeContainer;
    private OnPlayListActionClickListener mOnPlayListActionClickListener =null;
    private ImageView mPlayListOrderIv;
    private TextView mPlayListOrderTv;
    private View mPlayListOrderContainer;

    public ViewPopWindow(){
        super(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);//设置外部可点击
        //载入View
        mPopView = LayoutInflater.from(getAppContext()).inflate(R.layout.activity_player_pop_play_list, null);
        //设置内容
        setContentView(mPopView);
        //设置窗口进入和退出的动画
        setAnimationStyle(R.style.pop_animation);

        initView();
        initEvent();
    }

    private void initView() {
        mPlayListCloseBtn = mPopView.findViewById(R.id.play_list_close_btn);

        mPlayListRv = mPopView.findViewById(R.id.play_list_rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getAppContext());
        mPlayListRv.setLayoutManager(linearLayoutManager);
        mAdapterPlayerPlayListRv = new AdapterPlayerPlayListRv();
        mPlayListRv.setAdapter(mAdapterPlayerPlayListRv);

        mPlayListModeSwitchIv = mPopView.findViewById(R.id.play_list_mode_switch_iv);
        mPlayListModeSwitchTv = mPopView.findViewById(R.id.play_list_mode_switch_tv);
        mPlayListPalyModeContainer = mPopView.findViewById(R.id.play_list_paly_mode_container);

        mPlayListOrderIv = mPopView.findViewById(R.id.play_list_order_iv);
        mPlayListOrderTv = mPopView.findViewById(R.id.play_list_order_tv);
        mPlayListOrderContainer = mPopView.findViewById(R.id.play_list_order_container);
    }

    private void initEvent() {
        //点击关闭后，窗口消失
        mPlayListCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { ;
                ViewPopWindow.this.dismiss();
            }
        });

        mPlayListPalyModeContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换播放模式
                if (mOnPlayListActionClickListener != null) {
                    mOnPlayListActionClickListener.onPlayListModeSwitchClick();
                }
            }
        });

        mPlayListOrderContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换播放列表为顺序或者逆序
                if (mOnPlayListActionClickListener != null) {
                    mOnPlayListActionClickListener.onPlayListOrderClick();
                }
            }
        });

    }

    public void setPlayListData(List<Track>playListData){
        if (mAdapterPlayerPlayListRv != null) {
            mAdapterPlayerPlayListRv.setData(playListData);
        }
    }

    public void setCurrentPlayPosition(int position){
        if (mAdapterPlayerPlayListRv != null) {
            mAdapterPlayerPlayListRv.setCurrentPosition(position);
            mPlayListRv.scrollToPosition(position);//打开播放列表让当前播放的节目滑动到可视区域
        }
    }

    /**
     * 更新播放列表的播放模式UI
     * @param playMode
     */
    public void updatePlayListModeUI(XmPlayListControl.PlayMode playMode) {
        int resId = R.drawable.selector_play_mode_list_loop;
        int textId = R.string.play_mode_list_play_text;
        switch (playMode) {
            case PLAY_MODEL_LIST_LOOP:
                resId = R.drawable.selector_play_mode_list_loop;
                textId = R.string.play_mode_list_play_text;
                break;
            case PLAY_MODEL_RANDOM:
                resId = R.drawable.selector_play_mode_random;
                textId = R.string.play_mode_random_play_text;
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                resId = R.drawable.selector_play_mode_single_loop;
                textId = R.string.play_mode_single_play_text;
                break;
        }
        mPlayListModeSwitchIv.setImageResource(resId);
        mPlayListModeSwitchTv.setText(textId);
    }

    /**
     * 更新播放列表的顺序或者逆序UI
     * @param isReverse
     */
    public void updatePlayListOrderUI(boolean isReverse ){
        mPlayListOrderIv.setImageResource(isReverse?R.drawable.selector_play_list_order_reverse :R.drawable.selector_play_list_order);
        mPlayListOrderTv.setText(getAppContext().getResources().getString(isReverse?R.string.reverse_text:R.string.order_text));
    }


    public interface OnPlayListItemClickListener {
        void onItemClick(int postion);
    }
    public void setOnPlayListItemClickListener(OnPlayListItemClickListener playListItemClickListener){
        mAdapterPlayerPlayListRv.setOnItemClickListener(playListItemClickListener);
    }

    public interface OnPlayListActionClickListener {
        void onPlayListModeSwitchClick();//播放模式被点击了
        void onPlayListOrderClick();//播放顺序或者逆序被点击了
    }
    public void setOnPlayListActionClickListener(OnPlayListActionClickListener onPlayListActionClickListener){
        mOnPlayListActionClickListener = onPlayListActionClickListener;
    }

}
